package com.ruoyi.tbxt.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.tbxt.domain.BizSystem;
import com.ruoyi.tbxt.mapper.BizSystemMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BizSysServiceImpl implements IBizSysService{

    private static final Logger log = LoggerFactory.getLogger(BizSysServiceImpl.class);

    @Autowired
    private BizSystemMapper bizSystemMapper;

    @Override
    public List<BizSystem> selectBizSysList(BizSystem biz) {
        return bizSystemMapper.selectBizSysList(biz);
    }

    @Override
    public BizSystem selectBizById(Long id) {
        return bizSystemMapper.selectBizById(id);
    }

    @Override
    public BizSystem selectBizByName(String name) {
        return bizSystemMapper.selectBizByName(name);
    }

    @Override
    public BizSystem selectBizByCode(String code) {
        return bizSystemMapper.selectBizByCode(code);
    }

    @Override
    @Transactional
    public int deleteBizById(Long id) {
        return bizSystemMapper.deleteBizById(id);
    }

    @Override
    @Transactional
    public int deleteBizByIds(Long[] ids) {
        return bizSystemMapper.deleteBizByIds(ids);
    }

    @Override
    @Transactional
    public int deleteBizByIds(String ids) {
        Long[] bizIds = Convert.toLongArray(ids);
        return bizSystemMapper.deleteBizByIds(bizIds);
    }

    @Override
    @Transactional
    public int updateBiz(BizSystem biz) {
        return bizSystemMapper.updateBiz(biz);
    }

    @Override
    @Transactional
    public int insertBiz(BizSystem biz) {
        return bizSystemMapper.insertBiz(biz);
    }

    @Override
    public boolean checkBizCodeUnique(String code) {
        if(StringUtils.isNull(code))
            return UserConstants.NOT_UNIQUE;

        BizSystem biz = bizSystemMapper.checkBizCodeUnique(code);
        if(StringUtils.isNotNull(biz) && biz.getBizCode().equals(code)) {
            return UserConstants.NOT_UNIQUE;
        }

        return UserConstants.UNIQUE;
    }

    @Override
    public boolean checkBizCodeUnique(String code, Long bizId) {
        if(StringUtils.isNull(code))
            return UserConstants.NOT_UNIQUE;

        BizSystem biz = bizSystemMapper.checkBizCodeExistUnique(code, bizId);
        if(StringUtils.isNotNull(biz) && biz.getBizCode().equals(code)) {
            return UserConstants.NOT_UNIQUE;
        }

        return UserConstants.UNIQUE;
    }
}
